package com.simulation.intercepter;

import com.simulation.Service.ApiRequestLogService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: xh
 * @Date: 2019/8/17 07:09
 * @Description:
 */
public class ApiRequestLogInterceptor extends HandlerInterceptorAdapter {

    private ApiRequestLogService apiRequestLogService;

    public ApiRequestLogInterceptor(ApiRequestLogService apiRequestLogService) {
        this.apiRequestLogService = apiRequestLogService;
    }

    /**
     * @Description: 调用controller方法前的日志记录
     * @auther: xh
     * @param: [request, response, handler]
     * @return: boolean
     * @date: 2019/8/17 7:11
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        apiRequestLogService.preLog(request,response,handler);
        return true;
    }

    /**
     * @Description: 调用controller方法后的日志记录
     * @auther: xh
     * @param: [request, response, handler]
     * @return: boolean
     * @date: 2019/8/17 7:11
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        apiRequestLogService.postLog(request,response,handler);
        super.postHandle(request, response, handler, modelAndView);
    }

    /**
     * @Description: 调用controller方法抛出异常时日志记录
     * @auther: xh
     * @param: [request, response, handler]
     * @return: boolean
     * @date: 2019/8/17 7:11
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        apiRequestLogService.exceptionLog(request,response,handler,ex);
        super.afterCompletion(request, response, handler, ex);
    }

}
